package com.pospi.pai.yunpos.util;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.digi.common.ScaleData;
import com.digi.port.Device;
import com.digi.port.Printer;
import com.digi.port.Scale;
import com.pospi.util.DoubleSave;

import java.math.BigDecimal;

/**
 * Created by acer on 2018/8/27.
 * 寺冈顶电子秤
 */

public class ScaleUItil {

    private Context mContext;
    private boolean flag;

    public ScaleUItil(Context mContext) {
        this.mContext = mContext;
        flag = true;
    }

    public void openScale(){
        flag = true;
        Device.setContext(mContext);
        Scale.openScale();
        Printer.openPrinter1();
        Printer.openPrinter2();
    }
    public void touchTare(){
        Scale.oneTouchTare();
    }
    public void zeroScale(){
        Scale.digitalTare(new BigDecimal(0));
        Scale.zeroReset();
    }
    public void stopShow(){
        flag = false;
    }
    public void showWight(final EditText editText) {
        flag = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag) {
                    try {
                        ScaleData scaleData = Scale.getScaleData();
                    if (scaleData != null) {
                        //净重
                        final BigDecimal weight = scaleData.getWeight();
                        final boolean under = scaleData.underflowFg == 1;
                        final boolean over = scaleData.overflowFg == 1;
                        editText.post(new Runnable() {
                            @Override
                            public void run() {
                                if (under) {
                                    editText.setText("UF");
                                } else if (over) {
                                    editText.setText("OF");
                                } else {
                                    editText.setText(String.valueOf(weight));
                                }

                            }
                        });


                    } else {
                        flag = false;
                    }
                    } catch (Exception e) {
                        flag = false;
                    }
                    //间隔取一次重量信息
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();
    }
    public void showWight(final EditText editText, final TextView tv, final double price) {
        flag = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag) {
                    try {
                        ScaleData scaleData = Scale.getScaleData();
                        if (scaleData != null) {
                            //净重
                            final BigDecimal weight = scaleData.getWeight();
                            final boolean under = scaleData.underflowFg == 1;
                            final boolean over = scaleData.overflowFg == 1;
                            editText.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (under) {
                                        editText.setText("UF");
                                    } else if (over) {
                                        editText.setText("OF");
                                    } else {
                                        editText.setText(String.valueOf(weight));
                                        tv.setText(DoubleSave.doubleSaveTwo(Double.parseDouble(String.valueOf(weight)) * price) + "");
                                    }

                                }
                            });


                        } else {
                            flag = false;
                        }
                    } catch (Exception e) {
                        flag = false;
                    }
                    //间隔取一次重量信息
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();
    }


}
